The present invention relates generally to lock management in database management systems, and more particularly relates to a method and system for improving concurrency through early release of locks that are no longer required.
In database management systems, the transactions that form the basic building blocks of the system are commonly referred to as units of work. In executing a transaction or a unit of work, records from the data files of the database management system may be added, deleted, changed or read.
A database management system may temporarily be in an inconsistent state during a unit of work. Consider, for example, a customer information table that is being updated by a unit of work after a payment has been received from a customer. The unit of work comprises a first operation, in which the accounts receivable for customer A is reduced by the amount of the payment, and a second operation in which the cash receipts for customer A is increased by the amount of the payment. After the first operation has executed, but before the second operation has executed, the accounts receivable and cash receipts data are inconsistent in that they represent different states of affairs. The accounts receivable and cash receipts data will become consistent again if either the first operation rolls back, or the second operation executes.
A unit of work is a sequence of operations that is used by a database management system to ensure that data is in a consistent state. Any changes to data in a database management system are made during a unit of work and the results of the execution must be retained or not as a whole. Accordingly, a unit of work must end with either a COMMIT or a ROLLBACK statement. By the COMMIT statement, all of the changes within the unit of work are made permanent, while by the ROLLBACK statement, all of these changes are removed. In the above example, either both of the operations should be made permanent, or neither of the operations should be made permanent. If either operation is not successfully executed, then the data should be left as it was before the unit of work began.
A unit of work opens data through threads of access. In the above example, the first operation has a first thread of access to accounts receivable data, by means of which the accounts receivable data is updated, and the second operation has a second thread of access to cash receipts data by means of which the accounts receivable data is updated. Threads of access may be internal or external. In the case of an external thread of access, the user may directly control access to the information by, for example, controlling a user-defined cursor in an application. Alternatively, in the case of an internal thread of access, catalog information may be read while compiling a structured query language (SQL) statement.
In a database management system, data accessed in a unit of work needs to be protected against being accessed or changed by other units of work. Locks are used to provide this protection. Some locks can be released as soon as the data has been acquired, while other locks must be held until the end of the unit of work. A unit of work may have multiple threads of access to data open at the same time, and a piece of data may be accessed by more than one thread. When a particular thread of access for a unit of work ends, the unit of work cannot release the locks required by the particular thread of access if other threads of access for the same unit of work still need those locks for protection.
One approach is to hold all of the locks on all of the data until the unit of work terminates. However, this approach unnecessarily reduces concurrency as other units of work will have to wait until the present unit of work releases the lock despite the fact that the present unit of work does not actually need the lock.
Thus, a method and system for retaining a lock for a unit of work while such lock is required by at least one thread of access of the unit of work, and for releasing the lock when this condition is no longer met, are desirable.
An object of one aspect of the present invention is to provide an improved means for managing locks to a set of objects.
In accordance with one aspect of the present invention there is provided a lock control data structure for a unit of work of an application program. The unit of work has a set of threads of access for opening a set of objects accessible to the application program. The lock control data structure includes a set of object-specific lock models corresponding to the set of objects, and a lock controller for locking each object in a set of objects. Each object-specific lock model has an associated set of lock elements corresponding to the set of threads of access such that a thread-specific access requirement for each thread in the set of threads of access is representable by an associated lock element in the associated set of lock elements. Each object in the set of object has an associated object-specific lock model in the set of object-specific lock models wherein the associated set of lock elements of the associated object-specific lock model dynamically stores the thread specific access requirement of each thread in the set of threads relative to the object. The lock controller locks each object in the set of objects when the associated set of lock elements of the associated object-specific lock model is not empty. The lock controller unlocks each object in the set of objects when the associated set of lock elements of the associated object-specific lock model is emptied. The associated set of lock elements of the associated object-specific lock model is empty when it stores no locking requirements.
In accordance with another aspect of the present invention there is provided a computer software product for an application program having a unit of work. The unit of work has a set of threads of access for opening a set of objects accessible to the application program. The computer software program comprises a recording medium and the means recorded on the medium for configuring a computer to have a lock control data structure. The lock control data structure has a set of object-specific lock models corresponding to the set of objects, and a lock controller for locking each object in a set of objects. Each object-specific lock model has an associated set of lock elements corresponding to the set of threads of access such that a thread-specific access requirement for each thread in the set of threads of access is representable by an associated lock element in the associated set of lock elements. Each object in the set of object has an associated object-specific lock model in the set of object-specific lock models wherein the associated set of lock elements of the associated object-specific lock model dynamically stores the thread specific access requirement of each thread in the set of threads relative to the object. The lock controller locks each object in the set of objects when the associated set of lock elements of the associated object-specific lock model is not empty. The lock controller unlocks each object in the set of objects when the associated set of lock elements of the associated object-specific lock model is emptied. The associated set of lock elements of the associated object-specific lock model is empty when it stores no locking requirements.
In accordance with a further aspect of the present invention there is provided a data processing system for opening a set of objects accessible to the data processing system in the unit of work. The unit of work has a set of threads of access for opening the set of objects. The data processing system includes a lock control data structure having a set of object-specific lock models corresponding to the set of objects, and a lock controller for locking each object in a set of objects. Each object-specific lock model has an associated set of lock elements corresponding to the set of threads of access such that a thread-specific access requirement for each thread in the set of threads of access is representable by an associated lock element in the associated set of lock elements. Each object in the set of object has an associated object-specific lock model in the set of object-specific lock models wherein the associated set of lock elements of the associated object-specific lock model dynamically stores the thread specific access requirement of each thread in the set of threads relative to the object. The lock controller locks each object in the set of objects when the associated set of lock elements of the associated object-specific lock model is not empty. The lock controller unlocks each object in the set of objects when the associated set of lock elements of the associated object-specific lock model is emptied. The associated set of lock elements of the associated object-specific lock model is empty when it stores no locking requirements.